package zhtt.h2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhtt.h2.entity.User;
import zhtt.h2.repository.UserRepository;

import java.util.List;

@RestController
public class UserController {
   
    @Autowired
    private UserRepository userRepository;
   
    @RequestMapping("/save")
    public User save(@RequestBody User userInfo){
       userRepository.save(userInfo);
       return userInfo;
    }

    @RequestMapping("/save1")
    public String save1(){
       User userInfo = new User();
       userInfo.setName("张三");
       userInfo.setAge(20);
       userRepository.save(userInfo);

       userInfo = new User();
       userInfo.setName("李四");
       userInfo.setAge(30);
       userRepository.save(userInfo);

       return "ok";
    }
   
    @RequestMapping("/find")
    public List<User> find(){
       return (List<User>) userRepository.findAll();
    }
   
    @RequestMapping("/findByName")
    public User findByName(){
       return userRepository.findByName("张三");
    }
}